﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Web;
using System.Runtime.InteropServices;

namespace SCS.Common
{
    public class ComputeInfo
    {
        public static string getHostIpName()
        {
            string mac = "";
            ManagementClass mc;
            mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (mo["IPEnabled"].ToString() == "True")
                    mac = mo["MacAddress"].ToString();
            }
            return mac;
        }

        public static string getHostIP()
        {
            string mac = "";
            ManagementClass mc;
            mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (mo["IPEnabled"].ToString() == "True")
                    mac = mo["IPAddress"].ToString();
            }
            return mac;
        }

        public static string getRemoteIP()
        {
            return HttpContext.Current.Request.ServerVariables["Remote_Addr"].ToString();
        }

        public static string getRemoteHost()
        {
            return HttpContext.Current.Request.ServerVariables["Remote_Host"].ToString();
        }

        [DllImport("Iphlpapi.dll")]

        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);

        [DllImport("Ws2_32.dll")]

        private static extern Int32 iNet_addr(string ip);

        public static void getRemoteMAC()
        {

            // 在此处放置用户代码以初始化页面

            try
            {

                string userip = HttpContext.Current.Request.UserHostAddress;

                string strClientIP = HttpContext.Current.Request.UserHostAddress.ToString().Trim();

                Int32 ldest = iNet_addr(strClientIP); //目的地的ip

                Int32 lhost = iNet_addr("");   //本地服务器的ip

                Int64 macinfo = new Int64();

                Int32 len = 6;

                int res = SendARP(ldest, 0, ref macinfo, ref len);

                string mac_src = macinfo.ToString("X");

                if (mac_src == "0")
                {

                    //if (userip == "127.0.0.1")

                        //HttpContext.Current.Request.Write("正在访问Localhost!");

                   // else{}

                       // HttpContext.Current.Request.Write("欢迎来自IP为" + userip + "的朋友！" + "<br>");

                    return;

                }

                while (mac_src.Length < 12)
                {

                    mac_src = mac_src.Insert(0, "0");

                }

                string mac_dest = "";

                for (int i = 0; i < 11; i++)
                {

                    if (0 == (i % 2))
                    {

                        if (i == 10)
                        {

                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));

                        }

                        else
                        {

                            mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));

                        }

                    }

                }


            }

            catch (Exception err)
            {


            }

        }

    }
}
